/*
 * MapPrint.java
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package de.teamskill.pux.library.util;

import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.DataElement;
import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.OutputPort;

/**
 * MapPrint - prints out a Map structure (using the Object's toString() method)
 * and forward the DataElement on its output port
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class MapPrint extends AbstractNode {

	/**
	 * IN_MAP
	 */
	private static final String IN_MAP = "MAP";

	/**
	 * OUT_MAP
	 */
	private static final String OUT_MAP = "MAP";

	/**
	 * in
	 */
	private InputPort in;

	/**
	 * out
	 */
	private OutputPort out;

	/**
	 * MapPrint Constructor
	 * 
	 */
	public MapPrint() {
		this("MapPrint" + nr++, false);
	}

	/**
	 * MapPrint Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public MapPrint(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	@SuppressWarnings("unchecked")
	public void compute() {
		DataElement de = in.fetchBuffer();
		Map<Object, Object> m = (Map<Object, Object>) de.getData();
		Set<Entry<Object, Object>> s = m.entrySet();
		for (Entry<Object, Object> e : s) {
			System.out.println(e.getKey() + " : " + e.getValue());
		}
		out.pushBuffer(de);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	protected void createPorts() {
		in = addInputPort(IN_MAP, Map.class, true);
		out = addOutputPort(OUT_MAP, Map.class);
	}

	/**
	 * JavaBean-style getter-method for in
	 * 
	 * @return the in
	 */
	public InputPort getIn() {
		return in;
	}

	/**
	 * /** JavaBean-style setter-method for in
	 * 
	 * @param in
	 *            the in to set
	 */
	public void setIn(InputPort in) {
		this.in = in;
	}

	/**
	 * JavaBean-style getter-method for out
	 * 
	 * @return the out
	 */
	public OutputPort getOut() {
		return out;
	}

	/**
	 * /** JavaBean-style setter-method for out
	 * 
	 * @param out
	 *            the out to set
	 */
	public void setOut(OutputPort out) {
		this.out = out;
	}

}
